<?php

    namespace app\listen\model;

    use think\Exception;

    class Comment extends \app\listen\model\Base {
        public function commentList($voiceId, $toCommentId, $page = 1, $rows = 10) {

            try {
                $createSql = function () use ($voiceId, $toCommentId) {
                    $where = [];
                    if (isset($voiceId)) {
                        $where = ['v.voice_id' => $voiceId];
                    }
                    if (isset($toCommentId)) {
                        $where = ['v.to_comment_id' => $toCommentId];
                    }
                    return $this
                        ->alias("v")
                        ->join('listen_member m', 'v.uid=m.uid', 'LEFT')

                        ->where($where);
                };

                $total = $createSql()->field('count(1)')->count();
                $lists = $createSql()
                    ->field("v.*,m.nickname,m.head_photo,m.sex")
                    ->page($page, $rows)
                    ->order('v.addtime desc')
                    ->select();
                return [
                    'total'=>$total,
                    'list' => $lists
                ];
            } catch (Exception $e) {

                return [];
            }
        }

        // 检查添加评论的模型
        function valid($uid, $voiceId, $content, $toCommentId, $successCb, $failCb) {
            if (empty($uid)) {
                return $failCb('评论人id');
            } else if (empty($content)) {
                return $failCb('内容不能为空');
            } else if (empty($voiceId)) {
                return $failCb('必须填写语音id');
            } else {

                $model = [
                    'uid' => $uid,
                    'voice_id' => $voiceId,
                    'content' => $content,
                ];
                if (!empty($toCommentId)) {
                    $model['to_comment_id'] = $toCommentId;
                }
                $model['addtime'] = request()->time();
                return $successCb($model);
            }

        }

        public function addComment($uid, $voiceId, $content, $toCommentId) {
            return $this->valid($uid, $voiceId, $content, $toCommentId, function ($model) {
                try {
                    return $this->insert($model, false, true);
                } catch (Exception $e) {
                    return false;
                }

            }, function ($msg) {
                return $msg;
            });
        }

        public function delComment($id) {
            try {
                return $this->where(['id' => $id])->delete();
            } catch (Exception $e) {
                return false;
            }
        }

        // 喜欢评论
        public function loveComment($uid, $commentId) {
            try {
                return $this->table('listen_comment_love')->insert(['uid' => $uid, 'comment_id' => $commentId, 'addtime' => request()->time(),]);
            } catch (Exception $e) {
                return false;
            }

        }

        // 不喜欢
        public function disloveComment($uid, $commentId) {
            try {
                return $this->table('listen_comment_love')->where(['uid' => $uid, 'comment_id' => $commentId])->delete();
            } catch (Exception $e) {
                return false;
            }
        }

        // 喜欢总数
        public function getLoveCount($commentId) {
            try {
                return $this->table('listen_comment_love')->where([
                    'comment_id' => $commentId,
                ])->count();
            } catch (Exception $e) {
                return 0;
            }
        }

        public function getUseLove($uid, $comment) {
            try {
                return $this->table('listen_comment_love')->where([
                    'uid'=>$uid,
                    'comment_id'=>$comment,
                ])->find();
            } catch (Exception $e) {
                return null;
            }
        }

        public function getLovers($comment_id, $page = 1, $rows = 4) {
            try {
                $field = "v.*,m.nickname,m.head_photo,m.sex";
                return $this->alias("v")
                    ->join('listen_member m', 'v.uid=m.uid', 'LEFT')->field($field)
                    ->where([
                        'v.comment_id' => $comment_id,
                    ])
                    ->order('v.addtime desc')
                    ->page($page, $rows)
                    ->select();
            } catch (Exception $e) {
                return null;
            }
        }

        // 获取用户语音被喜欢的总数
        public function commentCount($voice_id) {
            if (!isset($voice_id)) return 0;
            try {
                return $this->where(['voice_id' => $voice_id])->count('1');
            } catch (Exception $e) {
                print_r($e);
                return 0;
            }
        }
    }